package functions;

import java.io.*;
import java.net.*;
import java.util.regex.*;
import connection.*;

public class Title implements IrcFunction {
	IrcConnection connection;
	String lastUrl=null;
	
	static Pattern titlePattern;
	static Pattern titleStartPattern;
	static Pattern titleEndPattern;
	
	public Title() {
		titlePattern = Pattern.compile("<title.*?>(.*?)</title.*?>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
		titleStartPattern = Pattern.compile("<title.*>", Pattern.CASE_INSENSITIVE);
		titleEndPattern = Pattern.compile("</title.*>", Pattern.CASE_INSENSITIVE);
	}
	public void setIrcConnection(IrcConnection con) {
		connection = con;
		connection.addEventListener(this, "PRIVMSG");
	}
	
	public void setParams(String[] params) {
	}
	
	public void eventPerformed(IrcEvent e) {
		if (e.getFirstWord().equals(",title")) {
			String title = findTitle(lastUrl);
			connection.privMsg(e.getTarget(), title);
			
		} else {
			String[] msg = e.getPrivMessage().split(" ");
			for (String word: msg) {
				if (word.startsWith("http://")) {
					lastUrl=word;
				} else if (word.startsWith("www.")) {
					lastUrl="http://"+word;
				}
			}
			
		}
	}
	
	public static String findTitle(String url_string) {
		BufferedReader in;
		String title="Could not find title";
		try {
			URL url = new URL(url_string);
			URLConnection conn = url.openConnection();
			in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            

			String inputLine;
			boolean inTitle=false;
			while ((inputLine = in.readLine()) != null) {
				if (titleStartPattern.matcher(inputLine).find()) {
					inTitle=true;
					title="";
				}
				if (inTitle) {
					title+=inputLine;
					if (titleEndPattern.matcher(inputLine).find()) {
						Matcher m = titlePattern.matcher(title);
						m.find();
						title = m.group(1);
						break;
					}
				}
			}
			
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return title;
	}
	
/*	public static String findTitle(String url) {
			String line;
			String title="";
			Pattern titlePattern = Pattern.compile("<title.*>(.*)</title.*>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
			Pattern titleStartPattern = Pattern.compile("<title.*>", Pattern.CASE_INSENSITIVE);
			Pattern titleEndPattern = Pattern.compile("</title.*>", Pattern.CASE_INSENSITIVE);
			Pattern endPattern = Pattern.compile("</head>|</html>", Pattern.CASE_INSENSITIVE);
			while(true) {
				line = in.readLine();
				if (endPattern.matcher(line).find()) {
					break;
				} else if (titleStartPattern.matcher(line).find()){
					title=line;
					while (!titleEndPattern.matcher(line).find()) {
						line += in.readLine();
					}
					break;
				}
			}
			Matcher m = titlePattern.matcher(title);
			m.find();
			title = m.group(1);
			return title;
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		return null;
	}*/

}
